package com.woniuxy.micromallserver.commoditymag.controller;

import com.alibaba.fastjson.JSONObject;
import com.woniuxy.common.bean.PageDto;
import com.woniuxy.common.bean.ResultMsg;
import com.woniuxy.micromallserver.bean.Commodity;
import com.woniuxy.micromallserver.commoditymag.service.CommodityService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Chan
 * @Date 2021/1/21 16:14
 */
@CrossOrigin
@RequestMapping("/commodity")
@RestController
public class CommodityController {

    @Resource
    private CommodityService commodityService;

    /**分页查询*/
    @GetMapping(value = "/getByPage")
    public ResultMsg getByPage(Integer pageNumber, Integer pageSize, Commodity commodity){
        ResultMsg resultMsg = new ResultMsg();
        PageDto pageDto = commodityService.findByPage(pageNumber, pageSize, commodity);
        if (pageDto == null) {
            return resultMsg.setCode(8899).setMessage("失败");
        }
        return resultMsg.setCode(200).setMessage("成功").setData(pageDto);
    }

    /**获取所有商品*/
    @GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getAllCommodities(){
        List<Commodity> allCommodities = commodityService.getAllCommodities();
        ResultMsg resultMsg = new ResultMsg(true);
        resultMsg.setData(allCommodities);
        return JSONObject.toJSONString(resultMsg);
    }

    /**根据id获取商品*/
    @GetMapping(value = "/getCommodityById/{commodityId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getCommodity(@PathVariable("commodityId") int commodityId){
        Commodity commodity = commodityService.getCommodityById(commodityId);
        ResultMsg resultMsg = new ResultMsg();
        resultMsg.setData(commodity);
        return JSONObject.toJSONString(resultMsg);
    }

    /**多条件查询*/
    @PostMapping(value = "/commoditiesConditions", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getCommoditiesConditions(@RequestBody Commodity commodity){
        List<Commodity> commoditiesByConditions =
                commodityService.getCommoditiesByMultiConditions(commodity);
        ResultMsg resultMsg = new ResultMsg(true);
        resultMsg.setData(commoditiesByConditions);

        return JSONObject.toJSONString(resultMsg);
    }

    /**新增商品*/
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addCommodity(@RequestBody Commodity commodity){
        int i = commodityService.addCommodity(commodity);
        if(i == 1){
            ResultMsg resultMsg = new ResultMsg(true);
            return JSONObject.toJSONString(resultMsg);
        }
        return JSONObject.toJSONString(new ResultMsg(false));
    }

    /**改变商品状态*/
    @PostMapping(value = "/{commodityId}/{commodityStatus}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String changeCommodityStatus(@PathVariable("commodityId") Integer commodityId, @PathVariable("commodityStatus") Integer commodityStatus){
        commodityService.changeCommodityStatus(commodityId, commodityStatus);
        return JSONObject.toJSONString(new ResultMsg(true));
    }

    /**编辑商品信息*/
    @PostMapping(value = "/editCommodity", produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateCommodity(@RequestBody Commodity commodity){
        commodityService.updateCommodity(commodity);
        return JSONObject.toJSONString(new ResultMsg(true));
    }
}
